<?php
$site_name = $_POST['site_name'];
$site_seo = $_POST['site_seo'];
$site_key = $_POST['site_key'];
$site_ms = $_POST['site_ms'];
$site_icp = $_POST['site_icp'];
$light_logo = $_POST['light_logo'];
$dark_logo = $_POST['dark_logo'];
$reuser = $_POST['reuser'];
$loginuser = $_POST['loginuser'];
$type = $_POST ['type'];

include_once("../../untils/conn.php");

if ($con) {
    mysqli_query($con, "set names utf8");
    
    // 查询数据库中的记录数
    $result = mysqli_query($con, "SELECT COUNT(*) FROM foundation WHERE type= '$type'");
    $row = mysqli_fetch_row($result);
    $num_rows = $row[0];
    
    if ($num_rows == 0) {
        // 如果记录数为0，说明数据库中没有数据，执行插入新数据的操作
        $data = $con->query("INSERT INTO foundation (site_name, site_seo, site_key, site_ms,site_icp, light_logo, dark_logo, reuser , loginuser, type) VALUES ('$site_name', '$site_seo', '$site_key', '$site_ms', '$site_icp', '$light_logo', '$dark_logo', '$reuser' , '$loginuser' , '$type')");
    } else {
        // 如果记录数不为0，说明数据库中已经存在数据，执行更新操作
        $data = $con->query("UPDATE foundation SET site_name='$site_name', site_seo='$site_seo', type='$type', site_key='$site_key', site_ms='$site_ms',  site_icp='$site_icp', light_logo='$light_logo', dark_logo='$dark_logo', reuser='$reuser', loginuser='$loginuser' WHERE id=1");
    }
    
    if ($data > 0) {
        echo "<script>alert('操作成功');window.location.href='../Foundation.php'</script> ";
    } else {
        echo ("<script>alert('操作失败');window.location.replace(document.referrer)</script>");
        // echo "Error updating record: " . $con->error;
    }
}

$con->close();
?>
